package com.example.a15476.myapplication;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    static private TextView textView;
    static private String time;
    static Handler handler = new Handler();//子线程通知主线程通知UI
    static Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(updateThread,1000);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//时间格式化
            time = dateFormat.format(Calendar.getInstance().getTime());
            textView.setText(time);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler.post(updateThread);
        textView = (TextView)findViewById(R.id.text);
    }
}
